package com.cat.model.web.action.admin.album;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.Page;
import com.cat.model.biz.service.admin.AlbumService;
import com.cat.model.common.model.common.AlbumModelVO;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.action.base.AdminAction;
import com.cat.model.web.bean.AlbumModelBean;

public class AlbumModelManagerAction extends AdminAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 425636316597986569L;

	private AlbumModelVO albumModel;
	private Long id;
	private String albumId;
	private AlbumModelBean albumModelBean;
		
	@Autowired
	private AlbumService albumService; 
	
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private ModelMapper modelMapper;
	
	/**
	 * list
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		if(albumId != null){
			if(albumId.contains(",")){
				String[] strs = albumId.split(",");
				albumId = strs[0];
			}
			condition.put("albumId", albumId);
		}
		
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchAlbumModelSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<AlbumModelBean> list = (List<AlbumModelBean>)plmCommDAO.search("manager.searchAlbumModel", condition, start, page.getNumPerPage());
		
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		return "list";
	}
	
	public String deleteSingle() {
		albumService.deleteAlbumModel(id);
		okAlert("删除成功！");
		return "list";
	}
	
	public String toAdd(){
		return "add";
	}
	
	public String add(){
		try {
			String modelId = request.getParameter("model.id");
			albumModel.setModelId(new Long(modelId));
			AlbumModelVO modelVO = modelMapper.getAlbumModelByType(albumModel.getAlbumId(), albumModel.getModelId(), albumModel.getType());
			if(modelVO!=null){
				parentAlert(false, "该专辑中本模特已存在");
				return "list";
			}
			
			albumService.createAlbumModel(albumModel);
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "list";
		}
		parentAlert(true, "保存成功");
		return "list";
	}
	
	public String toEdit(){
		albumModelBean = modelMapper.getAlbumModelBean(id);
		return "edit";
	}
	
	public String edit(){
		try {
			albumModel =  modelMapper.getAlbumModel(id);
			String modelId = request.getParameter("model.id");
			albumModel.setModelId(new Long(modelId));
			albumModel.setType(albumModelBean.getType());
			AlbumModelVO modelVO = modelMapper.getAlbumModelByType(albumModel.getAlbumId(), albumModel.getModelId(), albumModel.getType());
			if(modelVO!=null && !modelVO.getId().equals(albumModel.getId())){
				parentAlert(false, "该专辑中本模特已存在");
				return "list";
			}
			albumService.updateAlbumModel(albumModel);
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "list";
		}
		parentAlert(true, "保存成功");
		return "list";
	}

	public AlbumModelVO getAlbumModel() {
		return albumModel;
	}

	public void setAlbumModel(AlbumModelVO albumModel) {
		this.albumModel = albumModel;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getAlbumId() {
		return albumId;
	}

	public void setAlbumId(String albumId) {
		this.albumId = albumId;
	}

	public AlbumModelBean getAlbumModelBean() {
		return albumModelBean;
	}

	public void setAlbumModelBean(AlbumModelBean albumModelBean) {
		this.albumModelBean = albumModelBean;
	}

}
